package com.mlink.ai.chat.network

import com.mlink.ai.chat.utils.DeviceUtils
import com.cipher.CipherUtils
import com.cipher.CipherUtils.getMD5
import okhttp3.Interceptor
import okhttp3.Response

class CommonHeaderInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val uid = DeviceUtils.getUid()
        val timestamp: Int = (System.currentTimeMillis() / 1000).toInt()
        val source: String = timestamp.toString() + uid + CipherUtils.getCipherKeyFromJNI()
        val randKey: String = getMD5(source.toByteArray())
        val newRequest = originalRequest.newBuilder()
            .addHeader("Content-Type", "application/json")
            .addHeader("UID", uid)
            .addHeader("TIMESTAMP", timestamp.toString())
            .addHeader("RANDKEY", randKey)
            .build()

        val response = chain.proceed(newRequest)
//        val responseBody = response.peekBody(Long.MAX_VALUE)
        return response
    }
}
